﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using HtmlAgilityPack;
using MutilLanguage_Search.Controller;
namespace MutilLanguage_Search.Controller.GetKeyWords
{
    class GetContentAllLinksSyn
    {
        public static void Init(string link)
        {
            Console.WriteLine(link);
            WebClient webClient; webClient = new WebClient();
            webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(webClient_DownloadDataCompleted);
            webClient.DownloadDataAsync(new Uri(link));
        }
        static void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            try
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(ASCIIEncoding.UTF8.GetString(e.Result));
                String title = doc.DocumentNode.SelectSingleNode("//title").InnerText;
                title = HttpUtility.HtmlDecode(title);
                HtmlNodeCollection script = doc.DocumentNode.SelectNodes("//script");
                if (script != null)
                {
                    foreach (HtmlNode node in script)
                    {
                        try
                        {
                            node.ParentNode.RemoveChild(node);
                        }
                        catch { }
                    }
                }
                HtmlNodeCollection noscript = doc.DocumentNode.SelectNodes("//noscript");
                if (noscript != null)
                {
                    foreach (HtmlNode node in noscript)
                    {
                        try
                        {
                            node.ParentNode.RemoveChild(node);
                        }
                        catch { }
                    }
                }

                HtmlNodeCollection style = doc.DocumentNode.SelectNodes("//style");
                if (style != null)
                {
                    foreach (HtmlNode node in style)
                    {
                        try
                        {
                            node.ParentNode.RemoveChild(node);
                        }
                        catch { }
                    }
                }
                string body = doc.DocumentNode.SelectSingleNode("//body").InnerText;
                body = HtmlTagAnalysis.StripTagsCharArray(body);
                body = HttpUtility.HtmlDecode(body);
                String text = title + " " + body;
                if (text != null)
                {
                    Define.Variables.contentSyn += text;
                }
            }
            catch
            {

            }
            finally
            {
                Define.Variables.countLinkSyn++;
                if (Define.Variables.countLinkSyn == Data.Init.sdLinksSyn.Count)
                {
                    GetKeyWordsSyn.Analysis();
                }
            }
        }
    }
}
